package com.amkai.aes.webservice.binary.objecthandler.casehistory;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.amkai.aes.aescommon.service.binary.job.Job;
import com.amkai.aes.aescommon.service.binary.job.casehistory.SharedDictionaryJob;
import com.amkai.aes.aescommon.service.binary.job.casehistory.SharedDictionaryResult;
import com.amkai.aes.generatedbeans.dictionary.Item;
import com.amkai.aes.generatedbeans.inventory.items.SupplyResourceItem;
import com.amkai.aes.server.dictionary.SharedDictionaryManager;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;
import com.amkai.rad.designerbase.beans.RADBean;

public class SharedDictionaryJobHandler implements JobObjectHandler<SharedDictionaryJob, Serializable, SharedDictionaryResult> {
	private static final Log log = LogFactory.getLog(SharedDictionaryJobHandler.class);
	
	@Override
	public SharedDictionaryResult process(SharedDictionaryJob job, Serializable object) throws Exception {
		SharedDictionaryResult result = new SharedDictionaryResult();
		
		SessionManager sm = SiteManager.Start();
		sm.openSession();
		try {
			SharedDictionaryManager sdm = new SharedDictionaryManager(sm);
			result.setItem(sdm.getItemFromItemGroup(job.getItemKind(), job.getDepartmentId(), job.getItemGroupId()));
			if (result.getItem() != null) {
				result.getItem().getName();
				result.getItem().getId();
				result.getItem().getKind();
				result.getItem().getItemGroup();
				
				if (result.getItem() instanceof SupplyResourceItem) {
					if (((SupplyResourceItem) result.getItem()).getCategory() != null) {
						((SupplyResourceItem) result.getItem()).getCategory();
						((SupplyResourceItem) result.getItem()).getCategory().getName();
					}
				}
			}
		} catch (Exception exception) {
			SharedDictionaryJobHandler.log.error("Shared Dictionary Job", exception);
			exception.printStackTrace();
			throw exception;
		} finally {
			sm.closeSession();
		}
		
		return result;
	}

}
